package com.zhang.admin.service;

import com.zhang.admin.bean.Permission;
import com.zhang.admin.util.PermissionHelper;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @title:
 * @author: zhang
 * @date: 2023/3/28 下午1:37
 **/
public class RolePermissionService {

    /**
     * 根据角色id获取角色对应的所有权限
     * @param roleId
     * @return
     */
    public static List<Permission> getPermissionsByRoleId(Long roleId){
        try {
            Class.forName(DATABASE.DRIVER);
            var connection = DriverManager.getConnection(
                    DATABASE.URL,
                    DATABASE.USERNAME,
                    DATABASE.PASSWORD
            );
            var selectRolePermissionsStatement = connection.prepareStatement(
                    "select permission_id from role_permission where role_id = ?"
            );
            selectRolePermissionsStatement.setLong(1,roleId);
            var resultSet = selectRolePermissionsStatement.executeQuery();
            var allPermissions = new ArrayList<Long>();
            while (resultSet.next()){
                allPermissions.add(resultSet.getLong("permission_id"));
            }

            var selectPermissionStatement = connection.prepareStatement(
                    "select * from permission"
            );
            var permissionsResultSet = selectPermissionStatement.executeQuery();
            var permissions = new ArrayList<Permission>();
            while (permissionsResultSet.next()){
                var permission = new Permission();
                permission.setId(resultSet.getLong("id"));
                permission.setParentId(resultSet.getLong("parent_id"));
                permission.setPermissionName(resultSet.getString("permission_name"));
                permission.setPermissionCode(resultSet.getString("permission_code"));
                if (allPermissions.contains(permission.getId())){
                    permission.setSelect(true);
                }
                permissions.add(permission);
            }
            // 构建一个树形权限
            var permissionList = PermissionHelper.build(permissions);
            selectPermissionStatement.close();
            selectRolePermissionsStatement.close();
            connection.close();

            return permissionList;

        }catch (SQLException | ClassNotFoundException e){
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 添加角色权限关系，一个角色对应多个权限id
     * @param roleId
     * @param permissionIds
     */
    public static void addRolePermissions(Long roleId,List<Long> permissionIds){
        try {
            Class.forName(DATABASE.DRIVER);
            var connection = DriverManager.getConnection(
                    DATABASE.URL,
                    DATABASE.USERNAME,
                    DATABASE.PASSWORD
            );
            // 将角色roleId所对应的所有角色-权限表中的对应关系都删除
            var deleteStatement = connection.prepareStatement(
                    "delete from role_permission where role_id = ?"
            );
            deleteStatement.setLong(1,roleId);
            deleteStatement.execute();

            // 将角色和权限的关系插入到表中
            var insertStatement = connection.prepareStatement(
                    "insert into role_permission (role_id,permission_id) values (?,?)"
            );
            for (Long permissionId : permissionIds) {
                insertStatement.setLong(1,roleId);
                insertStatement.setLong(2,permissionId);
                insertStatement.execute();
            }
            deleteStatement.close();
            insertStatement.close();
            connection.close();
        }catch (SQLException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}
